﻿using HambergiteConquistador.Models;
using System;
using System.Collections.Generic;
using System.Security.Claims;
using System.Security.Principal;
using System.Web;
using System.Linq;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace HambergiteConquistador
{
    public partial class SiteMaster : MasterPage
    {
        private const string AntiXsrfTokenKey = "__AntiXsrfToken";
        private const string AntiXsrfUserNameKey = "__AntiXsrfUserName";
        private string _antiXsrfTokenValue;
        private const string DefaultImgPath = "/Uploaded_Files/default.jpg";

        protected void Page_Init(object sender, EventArgs e)
        {
            // The code below helps to protect against XSRF attacks
            var requestCookie = Request.Cookies[AntiXsrfTokenKey];
            Guid requestCookieGuidValue;
            if (requestCookie != null && Guid.TryParse(requestCookie.Value, out requestCookieGuidValue))
            {
                // Use the Anti-XSRF token from the cookie
                _antiXsrfTokenValue = requestCookie.Value;
                Page.ViewStateUserKey = _antiXsrfTokenValue;
            }
            else
            {
                // Generate a new Anti-XSRF token and save to the cookie
                _antiXsrfTokenValue = Guid.NewGuid().ToString("N");
                Page.ViewStateUserKey = _antiXsrfTokenValue;

                var responseCookie = new HttpCookie(AntiXsrfTokenKey)
                {
                    HttpOnly = true,
                    Value = _antiXsrfTokenValue
                };
                if (FormsAuthentication.RequireSSL && Request.IsSecureConnection)
                {
                    responseCookie.Secure = true;
                }
                Response.Cookies.Set(responseCookie);
            }

            Page.PreLoad += master_Page_PreLoad;
        }

        protected void master_Page_PreLoad(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                // Set Anti-XSRF token
                ViewState[AntiXsrfTokenKey] = Page.ViewStateUserKey;
                ViewState[AntiXsrfUserNameKey] = Context.User.Identity.Name ?? String.Empty;
            }
            else
            {
                // Validate the Anti-XSRF token
                if ((string)ViewState[AntiXsrfTokenKey] != _antiXsrfTokenValue
                    || (string)ViewState[AntiXsrfUserNameKey] != (Context.User.Identity.Name ?? String.Empty))
                {
                    throw new InvalidOperationException("Validation of Anti-XSRF token failed.");
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.ConfirmButton.Visible = false;
            this.CanselButton.Visible = false;
        }

        protected void Unnamed_LoggingOut(object sender, LoginCancelEventArgs e)
        {
            Context.GetOwinContext().Authentication.SignOut();
        }

        internal AspNetUser GetCurrentUser(string userName)
        {
            //if (this.Session["currentLogedUser"] == null)
            //{
                using (ConquistadorEntities context = new ConquistadorEntities())
                {
                    var user = context.AspNetUsers.FirstOrDefault(u => u.UserName == userName);
                    this.Session["currentLogedUser"] = user;
                }
            //}

            return this.Session["currentLogedUser"] as AspNetUser;
        }

        protected string GetUserPicture()
        {
            var username = Page.User.Identity.Name;
            var user = GetCurrentUser(username);
            string path = user.ImagePath;
            if (path == null)
            {
                path = DefaultImgPath;
            }
            else
            {
                path = user.ImagePath.Substring(1);
            }

            return path;
        }

        
        internal void StoreValueForAction(string value)
        {
            this.ConfirmButton.Visible = true;
            this.CanselButton.Visible = true;
            this.StoredValue.Text = value;
        }

                
        protected void ConfirmButton_Click(object sender, EventArgs e)
        {
            this.Session["ActionComfurmed"] =  true;
            this.ConfirmButton.Visible = false;
            this.CanselButton.Visible = false;
        }

        protected void CanselButton_Click(object sender, EventArgs e)
        {
            this.ConfirmButton.Visible = false;
            this.CanselButton.Visible = false;
        }
    }

}